package com.wny;

import android.location.Location;

//Event class handles parsing of event data from server

public class Event implements FormattedDisplay {
	private static String delim = "/";
	private static String tableName = "events";
	private static String nameField = "name";
	private String name;
	private Location loc;
	private int id;
	private String datetime;
	private String description;
	private String date;
	private String temp_date;
	private String temp_time;
	private String time;
	private String latitude;
	private String longitude;
	//Constructor for event class Uses '/' as the delimeter
	public Event(String attributeList) {
		String[] attributes = attributeList.split(delim);
		//id (event id), name, date, and event description are obtained from string the server
		id = Integer.valueOf(attributes[0]);
		name = attributes[1];
		datetime = attributes[2];
		description=attributes[5];
		
		String[] dateandtime=datetime.split(" ");
		temp_date=dateandtime[0];
		temp_time=dateandtime[1];
		latitude=attributes[3];
		longitude=attributes[4];
		
		TimeSplitter();
		DateSplitter();
	}

	public String getname(){
		return name;
	}
	public String getlatitude(){
		return latitude;
	}
	public String getlongitude(){
		return longitude;
	}
	private void TimeSplitter(){
		String[]full_time;
		full_time=temp_time.split(":");
		
		if( Integer.valueOf(full_time[0]) >12){
			int hour;
			hour=Integer.valueOf(full_time[0])-12;
			time=String.valueOf(hour)+":"+full_time[1]+" PM";
			
		}
		else if( Integer.valueOf(full_time[0]) <12){
			if(full_time[0].equals("00")){
				time=12+":"+full_time[1] + " AM";
			}
			else
			time=full_time[0]+":"+full_time[1] + " AM";
		}
		//else 12PM
		else {
			time="12"+":"+full_time[1] + " PM";
		}
		
	}
	private void DateSplitter(){
		String [] full_date;
		full_date=temp_date.split("-");
		date=full_date[1]+"/"+full_date[2]+"/"+full_date[0];
	}
	public String getdate(){
		return date;
	}
	public String gettime(){
		return time;
	}
	public String getdescription(){
		return description;
	}
	
	public String[] displayStrings() {
		// TODO Auto-generated method stub
		String[] toReturn = {name, description};
		return toReturn;
	}public int imageID() {
		return 0;
	}
	public int userID()
	{
		return -1;
	}
	public int eventID()
	{
		return id;
	}
	
	public final static String makeEventRepresentation(
			String eventName, String description, String lattitude, String longitude){
		StringBuilder temp = new StringBuilder("ade"+delim);
		temp.append(eventName + delim + description + delim + lattitude + delim + longitude + delim);
		return temp.toString();
	}
	//makes sure there are no conflicts when even is inserted
	public final static String checkConflicts(String eventRepresentation)
	{
		StringBuilder temp = new StringBuilder("select * from ");
		String[] args = eventRepresentation.split("" + delim);
		temp.append(tableName + " where "  + nameField + "= " + args[0]);
		return temp.toString();
	}
	//adds event into server
	public final static String insertEvent(String eventRepresentation)
	{
		StringBuilder temp = new StringBuilder("insert into ");
		String[] args = eventRepresentation.split("" + delim);
		temp.append(tableName + " values ('"+args[0]+"' '" + args[1]
		                        + "' '" + args[2] + "' '" + args[3] + "'");
		return temp.toString();
	}
}
